/*
 * FS.h
 *
 *  Created on: 9 May 2011
 *      Author: Marc Kampherbeek
 */

#ifndef FS_H_
#define FS_H_

#include "Inode.h"
#include "SuperBlock.h"
#include "DataBlock.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string>
#include <map>

class Directory;
class Datafile;

using namespace std;

#define CACHESIZE 1
#define SUPER_LOC 1
#define DSKBLKSIZ 512

class FS {
private:
	static FS* instance;
	char* device;
	Block* cache[CACHESIZE];
	SuperBlock* SBlock;
	const char* username;
	int userID;
	int groupID;
	//TODO: active inode table and open file table
	//active inode table needed to access data
	//open file table needed for open/close system calls
	//AIT: map<int, Inode>, int is inode number
	//OFT: map<string, DataFile>, string is file path
	map<int, Inode*>* AIT;
	map<int, Datafile*>* OFT;

public:
	FS(char*);
	void addFreeBlock(long);
	void determineUser();
	int getUserID(){ return userID; };
	int getGroupID(){ return groupID; };
	void setUserID(int newID){ userID = newID; };
	void setGroupID(int newID){ groupID = newID; };
	byte* readBlock(int);
	DataBlock* getDataBlock(int nr);
	int open(string,int,mode_t = 0);
	int close(int);
	ssize_t read(int, void*, size_t);
	ssize_t write(int, void*, size_t);
	off_t lseek(int, off_t, int);
	int creat(const char*, mode_t);
	int unlink(const char*);
	int remove(const char*);
	ssize_t write(Inode*, int, void*, size_t);
	void writeBlock(int, byte*, size_t);
	void writeInBlock(int, int, byte*, size_t);
	Inode* findInode(string);
	int getInodeNumber(string);
	virtual ~FS();
	static FS* getInstance();

	SuperBlock* getSuperBlock(){ return SBlock; };
};

#endif /* FS_H_ */
